/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.cstactity.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.client.cstactity.domain.Cstactity;
import qs.crm.modules.client.cstactity.repository.CstactityRepository;
import qs.crm.modules.client.cstactity.service.CstactityService;
import qs.crm.modules.client.cstactity.service.dto.CstactityDto;
import qs.crm.modules.client.cstactity.service.dto.CstactityQueryCriteria;
import qs.crm.modules.client.cstactity.service.mapstruct.CstactityMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-06-01
**/
@Service
@RequiredArgsConstructor
public class CstactityServiceImpl implements CstactityService {

    private final CstactityRepository cstactityRepository;
    private final CstactityMapper cstactityMapper;

    @Override
    public Map<String,Object> queryAll(CstactityQueryCriteria criteria, Pageable pageable){
        Page<Cstactity> page = cstactityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(cstactityMapper::toDto));
    }

    @Override
    public List<CstactityDto> queryAll(CstactityQueryCriteria criteria){
        return cstactityMapper.toDto(cstactityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CstactityDto findById(Long atvId) {
        Cstactity cstactity = cstactityRepository.findById(atvId).orElseGet(Cstactity::new);
        ValidationUtil.isNull(cstactity.getAtvId(),"Cstactity","atvId",atvId);
        return cstactityMapper.toDto(cstactity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CstactityDto create(Cstactity resources) {
        return cstactityMapper.toDto(cstactityRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Cstactity resources) {
        Cstactity cstactity = cstactityRepository.findById(resources.getAtvId()).orElseGet(Cstactity::new);
        ValidationUtil.isNull( cstactity.getAtvId(),"Cstactity","id",resources.getAtvId());
        cstactity.copy(resources);
        cstactityRepository.save(cstactity);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long atvId : ids) {
            cstactityRepository.deleteById(atvId);
        }
    }

    @Override
    public void download(List<CstactityDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CstactityDto cstactity : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户编号	外键，引用cst_customer表的主键", cstactity.getAtvCustNo());
            map.put("客户名称", cstactity.getAtvCustName());
            map.put("时间	", cstactity.getAtvDate());
            map.put("地点", cstactity.getAtvPlace());
            map.put("概要	", cstactity.getAtvTitle());
            map.put("详细信息", cstactity.getAtvDesc());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}